a few words about web development

PHP Error: unserialize() [function.unserialize]: Error at offset 118 of 512

Another straight to the point solution
Lately I got this error in my script while trying to decode serialized UTF-8 string:
unserialize() [function.unserialize]: Error at offset 118 of 512
Here's a simple solution I found. Instead of using unserialize() you should use mb_unserialize():
function mb_unserialize($str) 
{ 
    $res = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $str ); 
    return unserialize($res); 
}
You can also think about replacing in your program serialize() + unserialize() with json_encode() + json_decode(). Your data should be safer then.

Comments